#include "../include/Port_Global.h"
#include "../include/Portfolio.h"


void picmodule(char *name, DATAFILE *image, FONT *Japanese_Fonts)
{
//Reset Mouse Wheel
	position_mouse_z(0);
	scare_mouse();
//Generate Back Buffer	
	BITMAP *B_Buffer = create_bitmap(800,600);
//Generate Text for Game
	char direc[64] = "PORT/PIC/";
	strcat(direc,name);
	strcat(direc,"/");
	strcat(direc,name);
	strcat(direc,"_G.dat");

	//Load Graphic file for the game
	DATAFILE *GF = load_datafile(direc);
	
	int current_pic = 0;
	int mouseoldz = 0;
	int quit = 0;
	int lock = 0;
//NO TITLE just load images
	int state = 2;
	int collided = 0;
	int buttonroll = 0;
//Mouse checks
	int mouselock = 0;
	int mousecounter = 0;
//For the textscroller
	int scroll_position = 0;


//READ IN README TEXT FILE====================
	char bufin[256];		//size for text
	char text[50][256];
	fstream openf;

	strcpy(direc,"PORT/PIC/");
	strcat(direc,name);
	strcat(direc,"/DOC/");
	strcat(direc,name);
	strcat(direc,".txt");

	int line = 0;
	openf.open(direc,ios::in);

	while (!openf.eof() )
	{
		openf.getline (bufin,256);
		strcpy(text[line],bufin);
		line++;
	}

	for(int i = 3; i < 256; i++)	
		text[0][i-3] = text[0][i];
	

	openf.close();
	openf.clear();
//END READING

//BEGIN READING PICTURE DESCRIPTIONS
	char Picdesc[50][256];	//DYNAMIC ALLOC?
	int picnum;
	
	strcpy(direc,"PORT/PIC/");
	strcat(direc,name);
	strcat(direc,"/DESC.txt");

	openf.open(direc,ios::in);
//The first line desc.txt give how many pictures
	openf.getline(bufin,256);
	for(int i = 3; i < 256; i++)	
		bufin[i-3] = bufin[i];

	picnum = atoi(bufin);
//Then grab the descriptions
	int l = 0;
	while (!openf.eof() )
	{
		strcpy(Picdesc[l],bufin);
		strcpy( empty_string,bufin);
		openf.getline (bufin,50);
		ustrcpy(Picdesc[l],bufin);
		l++;
	}
	openf.close();
	openf.clear();


//Mouse hit detection image
	PMASK *submenubutton_pmask = create_allegro_pmask((BITMAP *)image[Submenubuttonpmask].dat);
	BITMAP *B_mousehit = create_bitmap(1,1);
	PMASK *mousehit_pmask = create_allegro_pmask(B_mousehit);

	BITMAP *B_Reverse = create_bitmap(64,64);
	clear_to_color(B_Reverse,makecol(255,255,0));
	draw_sprite_h_flip(B_Reverse,(BITMAP *)image[Nextpic].dat,0,0);

	PMASK *nextpic_f_pmask = create_allegro_pmask(B_Reverse);			//flipped pmask
	PMASK *nextpic_pmask   = create_allegro_pmask((BITMAP *)image[Nextpic].dat);	//normal



	enum Pic_Button {	PicButXA = 20,
						PicButY  = 400,
						PicButXB = 716};

//Start Main Loop
	while(!key[KEY_ESC] && quit == 0)//If the user hits escape, quit the program
	{
		while(Portfolio::ticks > 0)
		{
			if(mouselock == 1)
			{
				if(mouse_b == 0)
					mouselock = 0;
			}

			if(mouselock == 2)
			{
				mousecounter++;
				if(mousecounter > 10)
				{
					mousecounter = 0;
					mouselock= 0;
				}
			}

			collided = check_pmask_collision(submenubutton_pmask,mousehit_pmask,38,510,mouse_x,mouse_y);

			if( collided == 1)
			{
				if(mouse_x < 90)
					buttonroll = 1;
				if(mouse_x > 100 && mouse_x < 220)
					buttonroll = 2;
				if(mouse_x > 248)
					buttonroll = 3;
			}
			else
			{
				buttonroll = 0;
			}

			if(state == 2)
			{
				if(check_pmask_collision(nextpic_pmask,mousehit_pmask,PicButXA,PicButY,mouse_x,mouse_y))
				{
					if(mouse_b == 1 && mouselock == 0)
					{
						mouselock = 1;
						current_pic--;
						if(current_pic < 0)
							current_pic = picnum - 1;
					}
				}

				if(check_pmask_collision(nextpic_f_pmask,mousehit_pmask,PicButXB,PicButY,mouse_x,mouse_y))
				{
					if(mouse_b == 1 && mouselock == 0)
					{
						mouselock = 1;
						current_pic = (current_pic + 1) % picnum;
					}
				}
			}

//Exit back to previous menu
			if(buttonroll == 1 && mouse_b ==1 && mouselock == 0)
			{
				mouselock = 1;
				quit = 1;
			}
			if(buttonroll == 2 && mouse_b == 1 && mouselock == 0)
			{	
				mouselock = 1;
				state = 2;				
			}

			if(buttonroll == 3 && mouse_b == 1 && mouselock == 0)
			{
				mouselock = 1;
				state = 1;
			}

			Portfolio::ticks --;
		}//End while(Portfolio::ticks > 0)


//==========================================BEGIN ON SCREEN DRAWING==========================================================//
//===========================================================================================================================//
		acquire_screen();
//DRAW GUI BUTTONS
		draw_sprite(B_Buffer,(BITMAP *)image[Submenu].dat,0,0);

		if(buttonroll == 1)
			blit((BITMAP *)image[Submenubutton].dat,B_Buffer,0,0,38,510,70,76);
		if(buttonroll == 2)
			blit((BITMAP *)image[Submenubutton].dat,B_Buffer,70,0,108,510,141,76);
		if(buttonroll == 3)
			blit((BITMAP *)image[Submenubutton].dat,B_Buffer,210,0,248,510,144,76);
		
		if(state == 2)
		{
			fblend_rect_trans  	(B_Buffer,50,50,700,500,makecol(0,0,0),90);
			fblend_rect_trans  	(B_Buffer,52,12,700,30,makecol(0,0,0),90);
			fblend_rect_trans  	(B_Buffer,50,10,700,30,makecol(0,0,255),90);

			set_alpha_blender();
			
			draw_trans_sprite(B_Buffer,B_Reverse,PicButXA,PicButY);
			draw_trans_sprite(B_Buffer,(BITMAP *)image[Nextpic].dat,PicButXB,PicButY);
			solid_mode();

			BITMAP * temp = (BITMAP *)GF[current_pic].dat;

			draw_sprite(B_Buffer,(BITMAP *)GF[current_pic].dat,(800-(temp->w))/2,(600-(temp->h))/2);


			textprintf(B_Buffer,Japanese_Fonts,52,12,makecol(255,255,255),Picdesc[current_pic]);
		}
		
//Instruction View Mode		
		if(state == 1)
		{
			int linetrip;
			int moused = 0;
			int dx = BUTA_X - mouse_x;
			int dy = BUTA_Y - mouse_y;

			fblend_rect_trans(B_Buffer,BUTA_X,BUTA_Y,701,(BUTB_Y - BUTA_Y)+1,C_TextBox,200);	
			vline(B_Buffer, BUTA_X, BUTA_Y, BUTB_Y, makecol(255,255,255));
						
			//Draw the horizontal scroll bar.
			vline(B_Buffer, BUTA_X  , BUTA_Y, BUTB_Y, C_ScrollBar);
			vline(B_Buffer, BUTA_X+1, BUTA_Y, BUTB_Y, C_ScrollBar);
			vline(B_Buffer, BUTA_X-1, BUTA_Y, BUTB_Y, C_ScrollBar);

			if(line < MAXLINE)
				linetrip = (BUTB_Y - BUTA_Y);
			else
				linetrip = (BUTB_Y - BUTA_Y)/(line-MAXLINE);

			vline(B_Buffer,BUTA_X, BUTA_Y + linetrip * scroll_position, BUTA_Y + linetrip * scroll_position + linetrip,C_ScrollBarSmall);
//CALC DISTANCE
			moused = (dx*dx) + (dy*dy);
			triforce(B_Buffer,BUTA_X,BUTA_Y,C_ScrollBarButton);
//TEXT SCROLLER
			if(moused < 49)
			{
				triforce(B_Buffer,BUTA_X,BUTA_Y,C_ScrollBarButtonHit);
				if(mouse_b == 1 && mouselock == 0 && line > MAXLINE)
				{
					scroll_position--;
					if(scroll_position < 0)
						scroll_position = 0;
					mouselock = 2;
				}
			}

			dx = BUTB_X - mouse_x;
			dy = BUTB_Y - mouse_y;
//CALC DISTANCE
			moused = (dx*dx) + (dy*dy);			
			triforce(B_Buffer,BUTA_X,BUTB_Y,C_ScrollBarButton,-10);
			if(moused < 49)
			{
				triforce(B_Buffer,BUTA_X,BUTB_Y,C_ScrollBarButtonHit,-10);
				if(mouse_b == 1 && mouselock == 0 && line > MAXLINE)
				{
					scroll_position++;
					if(scroll_position > (line-1) - MAXLINE)		
						scroll_position = (line-1) - MAXLINE;
					mouselock = 2;
				}
			}
//WHEEL MOUSE
			if(mouse_z < mouseoldz && line > MAXLINE)
			{
				scroll_position++;
				if(scroll_position > (line-1) - MAXLINE)			
					scroll_position = (line-1) - MAXLINE;
				mouseoldz = mouse_z;
			}
			if(mouse_z > mouseoldz && line > MAXLINE)
			{
				scroll_position--;
				if(scroll_position < 0)
					scroll_position = 0;
				mouseoldz = mouse_z;
			}

			for(int i = scroll_position; i <= scroll_position + MAXLINE && i <= line ; i++)
				textprintf(B_Buffer, Japanese_Fonts,70,(i-scroll_position)*20+BUTA_Y+5, C_SetsumeiText,text[i]);

			textprintf(B_Buffer, Japanese_Fonts,90,490, C_SetsumeiText,defaultmsg[9]);			
		}


	



		moduletexthelp(B_Buffer,Japanese_Fonts,buttonroll);
		rectfill(B_Buffer,mouse_x - 3,mouse_y - 3,mouse_x + 3, mouse_y + 3, makecol(0,0,0));	
		rectfill(B_Buffer,mouse_x - 2,mouse_y - 2,mouse_x + 2, mouse_y + 2, makecol(255,255,255));		
		
		textprintf(B_Buffer, Japanese_Fonts,20,20, makecol(255,0,0),"%d",picnum);
		textprintf(B_Buffer, Japanese_Fonts,20,20, makecol(255,0,0),"%d",current_pic);
		blit(B_Buffer,screen,0,0,0,0,800,600);
		release_screen();
	}
	
	destroy_pmask(nextpic_f_pmask);
	destroy_pmask(nextpic_pmask);
	destroy_pmask(mousehit_pmask);
	release_bitmap(B_mousehit);
	destroy_pmask(submenubutton_pmask);
	unload_datafile(GF);
	release_bitmap(B_Buffer);
	release_bitmap(B_Reverse);
	clear_keybuf();

}
